package vista;
import com.cloudgarden.layout.AnchorLayout;

import contoladores.COrden;
import contoladores.CProducto;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.util.Iterator;
import java.util.Vector;

import javax.swing.*;
import javax.swing.border.LineBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;



/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class VOrden extends javax.swing.JFrame {
	private JLabel jLblcod_categoria;
	private JButton jButtonCancelar;
	private JLabel jLabel1;
	private JButton Salir;
	private JButton jButtonAgregar;
	private JTextField jTxtci;
	private JButton jButtonMenos;
	private JButton jButtonMas;
	private JSpinner jSpinnerCantidad;
	private JLabel jLabel8;
	private JLabel jLabel4;
	private JComboBox jComboBoxProducto;
	private JLabel jLabelToltal;
	private JLabel jLabel7;
	private JLabel jLabel3;
	private JTextField jTextFieldNombre;
	private JLabel jLabel2;
	private JDialog jDialog1;
	private JFrame jFrame1;
	private JWindow jWindow1;
	private JLabel jLabelaleatorio;
	private JTable jTable1;
	private JLabel jLbllIngredientes;
	private JLabel jLbldescripcion;
	private JLabel jLblDescripcion;
	private DefaultTableModel modelo;
	private COrden orden;

	
	public VOrden(COrden ord)  {
		super();
		initGUI();
		orden = ord;
		
	}
	private void initGUI() {
		{
			getContentPane().setLayout(null);
			this.setTitle("Orden");
		}
		{
			jLbldescripcion = new JLabel();
			getContentPane().add(jLbldescripcion, "North");
			FlowLayout jLbldescripcionLayout = new FlowLayout();
			jLbldescripcion.setText("C.I.:");
			jLbldescripcion.setLayout(null);
			jLbldescripcion.setBounds(71, 114, 37, 15);
		}
		{
			jTxtci = new JTextField();
			getContentPane().add(jTxtci, "West");
			jTxtci.setBounds(144, 104, 106, 22);
			jTxtci.setForeground(new java.awt.Color(255,0,0));
		}
		{
			jButtonAgregar = new JButton();
			getContentPane().add(jButtonAgregar);
			jButtonAgregar.setText("Agregar");
			jButtonAgregar.setBounds(85, 348, 95, 22);
			jButtonAgregar.setActionCommand("Agregar");
		}
		{
			Salir = new JButton();
			getContentPane().add(Salir);
			Salir.setBounds(301, 400, 86, 22);
			Salir.setText("Salir");
			Salir.setActionCommand("Salir");
		}
		{
			jLabel1 = new JLabel();
			getContentPane().add(jLabel1);
			jLabel1.setText("Total:");
			jLabel1.setBounds(261, 325, 41, 15);
		}
		{
			jButtonCancelar = new JButton();
			getContentPane().add(jButtonCancelar);
			jButtonCancelar.setText("Limpiar");
			jButtonCancelar.setBounds(191, 348, 107, 22);
			jButtonCancelar.setActionCommand("Cancelar");
		}
		{
			jLbllIngredientes = new JLabel();
			getContentPane().add(jLbllIngredientes);
			jLbllIngredientes.setText("Productos Solicitados:");
			jLbllIngredientes.setBounds(39, 242, 327, 15);
		}
		{
			
			TableModel jTable1Model = 
				new DefaultTableModel(
						new String[][] { { "One", "Two" }, { "Three", "Four" } },
						new String[] { "Column 1", "Column 2" });
			jTable1 = new JTable();
			getContentPane().add(jTable1);
			jTable1.setModel(jTable1Model);
			jTable1.setBounds(93, 269, 234, 44);
			jTable1.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
			jTable1.setNextFocusableComponent(this);
		}
		{
			jLabelaleatorio = new JLabel();
			getContentPane().add(jLabelaleatorio);
			jLabelaleatorio.setBounds(104, 19, 70, 23);
			jLabelaleatorio.setForeground(new java.awt.Color(255,0,0));
			jLabelaleatorio.setText("# aleatorio");
		}
		{
			jLabel2 = new JLabel();
			getContentPane().add(jLabel2, "North");
			jLabel2.setText("Nombre:");
			jLabel2.setLayout(null);
			jLabel2.setBounds(71, 141, 61, 15);
		}
		{
			jTextFieldNombre = new JTextField();
			getContentPane().add(jTextFieldNombre);
			jTextFieldNombre.setBounds(144, 138, 238, 22);
		}
		{
			jLabel3 = new JLabel();
			getContentPane().add(jLabel3);
			jLabel3.setText("Datos Cliente:");
			jLabel3.setBounds(31, 80, 119, 15);
		}
		
		{
			jLabel7 = new JLabel();
			getContentPane().add(jLabel7);
			jLabel7.setText("Orden #:");
			jLabel7.setBounds(31, 23, 61, 15);
		}
		{
			jLabelToltal = new JLabel();
			getContentPane().add(jLabelToltal);
			jLabelToltal.setBounds(319, 319, 80, 29);
		}
		{
			ComboBoxModel jComboBoxProductoModel = 
				new DefaultComboBoxModel(
						new String[] { "Item One", "Item Two" });
			jComboBoxProducto = new JComboBox();
			getContentPane().add(jComboBoxProducto);
			jComboBoxProducto.setModel(jComboBoxProductoModel);
			jComboBoxProducto.setBounds(144, 181, 133, 22);
		}
		{
			jLabel4 = new JLabel();
			getContentPane().add(jLabel4);
			jLabel4.setText("Productos");
			jLabel4.setBounds(71, 185, 64, 15);
		}
		{
			jLabel8 = new JLabel();
			getContentPane().add(jLabel8);
			jLabel8.setText("Cantidad");
			jLabel8.setBounds(289, 185, 65, 15);
		}
		{
			Vector<Integer> v = new Vector<Integer>();
			int i =1;
			while(i<=100){
				v.add(i);
				i++;
			}
			SpinnerListModel jSpinnerCantidadModel = 
				new SpinnerListModel(
						new String[] { "Sun", "Mon" , "Tue" , "Wed" , "Thu" , "Fri" , "Sat" });
			jSpinnerCantidad = new JSpinner();
			jSpinnerCantidadModel.setList(v);
			getContentPane().add(jSpinnerCantidad);
			jSpinnerCantidad.setModel(jSpinnerCantidadModel);
			jSpinnerCantidad.setBounds(366, 182, 45, 22);
		}
		{
			jButtonMas = new JButton();
			getContentPane().add(jButtonMas);
			jButtonMas.setText("+");
			jButtonMas.setBounds(301, 216, 44, 22);
			jButtonMas.setActionCommand("Mas");
		}
		{
			jButtonMenos = new JButton();
			getContentPane().add(jButtonMenos);
			jButtonMenos.setText("-");
			jButtonMenos.setBounds(358, 216, 42, 22);
			jButtonMenos.setActionCommand("Menos");
		}

		{
			this.setSize(421, 476);
		}
		modelo = new DefaultTableModel();
		jTable1.setModel(modelo);
		Vector<String> v = new Vector<String>();
		v.add("Producto");
		v.add("Precio");
		v.add("Cantidad");
		v.add("Sub-Total");
		modelo.setColumnIdentifiers(v);
	}
	
	public void addListener(ActionListener escuchador){
		jButtonAgregar.addActionListener(escuchador);
		jButtonMas.addActionListener(escuchador);
		jButtonMenos.addActionListener(escuchador);
		Salir.addActionListener(escuchador);
		jButtonCancelar.addActionListener(escuchador);
	}
	
	public int getCantidad(){
		String nf = ""+jSpinnerCantidad.getValue();		
		return Integer.parseInt(nf);
		
	}
	
	public void limpiar(String cod, String fecha){
		jTextFieldNombre.setText("");
		jTxtci.setText("");
		jComboBoxProducto.setSelectedIndex(0);
		/*TableModel jTable1Model = 
			new DefaultTableModel();
		modelo= new DefaultTableModel();
		jTable1.setModel(modelo);*/
	}
	
	public void setCodigoOrdenAleatorio(String codigo){
		jLabelaleatorio.setText(codigo);
	}
	
	public  void  cargarProductos(Vector<String> v){
		jComboBoxProducto.removeAllItems();
		for (Iterator iterator = v.iterator(); iterator.hasNext();) {
			String string = (String) iterator.next();
			jComboBoxProducto.addItem(string);
	}}
	

	public  String getCi(){
		return  jTxtci.getText();
	}
	
	public String getNom(){
		return jTextFieldNombre.getText();
	}

	public void cargarIngredientes(Vector<String> v) {
		jComboBoxProducto.removeAllItems();
		for (Iterator iterator = v.iterator(); iterator.hasNext();) {
			String string = (String) iterator.next();
			jComboBoxProducto.addItem(string);
		}
	}
	public  String getPro(){
		return  (String) jComboBoxProducto.getItemAt(jComboBoxProducto.getSelectedIndex());
	}
		
		public void agregar(double precio){
			String d = getPro();  
			String c = ""+getCantidad();
			int i =0;
			while(i<modelo.getRowCount()){
				if(d.equals((String) modelo.getValueAt(i, 0))){
					modelo.setValueAt(c, i, 2);
					modelo.setValueAt((getCantidad()*precio), i, 3);	
					total();
					return;
				}
				i++;
			}
			Vector v = new Vector();
			v.add(d);
			v.add(precio);
			v.add(c);
			v.add((getCantidad()*precio));
			modelo.addRow(v);
			total();
		}
		
		public  String getCod(){
			return  jLabelaleatorio.getText();
		}
		public  String getTot(){
			return  jLabelToltal.getText();
		}
		
		
		public DefaultTableModel getModelo() {
			return modelo;
		}

		public void setModelo(DefaultTableModel modelo) {
			this.modelo = modelo;
		}

		private void total(){
		
		    double total=0;
			int i =0;
			while(i<modelo.getRowCount()){
				System.out.println(modelo.getValueAt(i, 3));
				total+=(Double)modelo.getValueAt(i, 3);					
				i++;
			}
			jLabelToltal.setText(""+total);
		}
		
		public void quitar(){
			int i =jTable1.getSelectedRow();
			if(i!=-1){				
				double t = Double.parseDouble(jLabelToltal.getText()) -(Double)modelo.getValueAt(i, 3);
				jLabelToltal.setText(""+t);
				modelo.removeRow(i);
				total();
			}
		}
	
		

}
